package com.example.moonshine.zxing.Fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Looper;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

//import com.example.moonshine.zxing.Activity.LaunchActivity;
import com.example.moonshine.zxing.Activity.LaunchActivity;
import com.example.moonshine.zxing.Activity.LoginActivity;
import com.example.moonshine.zxing.Activity.PersonalActivity;
import com.example.moonshine.zxing.Activity.Setpsw;
import com.example.moonshine.zxing.Activity.banbenActivity;
import com.example.moonshine.zxing.Data.SidSelectData;
import com.example.moonshine.zxing.Hepler.DataDBHepler;
import com.example.moonshine.zxing.R;
import com.example.moonshine.zxing.Util.CircleImageView;
import com.example.moonshine.zxing.Util.SavePamasInfo;
import com.example.moonshine.zxing.Util.SelecthttpUserUtil;

import java.util.ArrayList;

import static android.content.ContentValues.TAG;

/**
 * Created by moonshine on 2018/2/4.
 */
public class SecondFragment extends Fragment {
    private View view;
    CircleImageView mCircleImageView;
    String          logoutUrl = "http://119.23.219.22:80/element-admin/user/logout";
    SavePamasInfo   mSavePamasInfo = new SavePamasInfo();
    @SuppressLint("SetTextI18n")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fg2, container, false);
        mCircleImageView = view.findViewById(R.id.h_head);
        @SuppressLint("WrongViewCast")
        TextView tv_userCode = view.findViewById(R.id.userCode);
        String code = mSavePamasInfo.getInfo(getContext(),"userCode","login");
        if (code.equals("")){
            tv_userCode.setText("工号："+"RBI0000");
        }else {
            tv_userCode.setText("工号："+code);

        }
        TextView tv =(TextView) view.findViewById(R.id.change_paw);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(),Setpsw.class));
                getActivity().overridePendingTransition(R.anim.up_in, R.anim.up_out);
            }
        });
        TextView tv1=(TextView) view.findViewById(R.id.pensonal);
        tv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(),PersonalActivity.class));
                getActivity().overridePendingTransition(R.anim.up_in, R.anim.up_out);
            }
        });
        TextView tv2 = view.findViewById(R.id.we);
        tv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(),banbenActivity.class));
                getActivity().overridePendingTransition(R.anim.up_in, R.anim.up_out);
            }
        });

        /**
         *  登出
         */

        final SelecthttpUserUtil selecthttpUserUtil = new SelecthttpUserUtil();
        Button btn_out = (Button)view.findViewById(R.id.login_out);
        btn_out.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Looper.prepare();
                       try {
                           String SID = SelectSid();
                           selecthttpUserUtil.postSidhttp(SID,logoutUrl);
                           DataDBHepler dataDBHepler = new DataDBHepler(getContext());
                           dataDBHepler.delete("1");
                           startActivity(new Intent(getActivity(), LaunchActivity.class));
                           getActivity().finish();
                       }catch (Exception e)
                       {
                           e.printStackTrace();
                       }

                        Looper.loop();
                    }
                }).start();
            }

        });

        return view;
    }

    /**
     * @param keyCode
     * @param event   监听手机back键 点击返回界面
     * @return
     */
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            //启动一个意图,回到桌面
            Intent backHome = new Intent(Intent.ACTION_MAIN);
            backHome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            backHome.addCategory(Intent.CATEGORY_HOME);
            startActivity(backHome);
            return true;
        }
        return view.onKeyDown(keyCode, event);
    }


    /**
     * 查询数据库里的sid
     */
    public String SelectSid(){

        DataDBHepler dataDBHepler = new DataDBHepler(getContext());
        ArrayList<SidSelectData> DataList = dataDBHepler.FindSidData();
        final SidSelectData data = new SidSelectData(DataList.get(0).getId(),DataList.get(0).getSid(),DataList.get(0).getSysids());

        final String Msid = data.getSid();//获取数据库里的sid
        return Msid;
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i("tag","在线");
        String path = null;
        try {
            path = mSavePamasInfo.getInfo(getContext(),"imgPath","file");
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (path.equals("")){

            mCircleImageView.setImageResource(R.drawable.ic_header);
        }else {
            Bitmap bm = BitmapFactory.decodeFile(path);
            mCircleImageView.setImageBitmap(bm);
        }
    }
}
